//
//  DRFoodPairingDataModel.m
//  DoctorRen
//
//  Created by Lemon on 2018/11/22.
//

#import "DRFoodPairingDataModel.h"
#import "DRLoginDataModel.h"


@implementation DRFoodPairingDataModel

+(void)loadFoodPairingDataWithType:(NSInteger)type andSuccess:(void (^)(DRFoodPairingModel * _Nonnull))succBlock andFail:(RequestFail)failBlock{
    [[DRHttpTool shared] new_POST:DRURL(@"index.php/NewDietphan/personal_catering") andParams:@{@"token": [DRLoginDataModel getUserToken] } andSucc:^(id  _Nonnull data) {
        if (succBlock) {
            DRFoodPairingModel *model = [DRFoodPairingModel yy_modelWithJSON:data];
            succBlock(model);
        }
        
    } andFail:^(NSError * _Nonnull error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}

+ (void)matchingFoodWithType:(NSInteger)type andMealType:(NSInteger )mealType andSuccess:(void (^)(void))succBlock andFail:(RequestFail)failBlock{
    NSDictionary *params = @{
                             @"token": [DRLoginDataModel getUserToken],
                             @"cycle": @(type),
                             @"ctcp" : [self getMealTypeWithIndex:mealType]
                             };
    [[DRHttpTool shared] new_POST:DRURL(@"index.php/NewDietphan/food_matching") andParams:params andSucc:^(id  _Nonnull data) {
        if (succBlock) {
            succBlock();
        }
        
    } andFail:failBlock];
}

+ (void)zizhu_matchingFoodWithType:(NSInteger)type andMealType:(NSInteger)mealType andSuccess:(void (^)(void))succBlock andFail:(RequestFail)failBlock{
    NSDictionary *params = @{
                             @"token": [DRLoginDataModel getUserToken],
                             @"cycle": @(type),
                             @"ctcp" : [self getMealTypeWithIndex:mealType]
                             };
    [[DRHttpTool shared] new_POST:DRURL(@"index.php/NewDietphan/nc_matching") andParams:params andSucc:^(id  _Nonnull data) {
        if (succBlock) {
            succBlock();
        }
        
    } andFail:failBlock];
}

+ (void)getEnergyWithType:(NSInteger)type andSuccess:(void (^)(DREnergyTipModel * _Nonnull))succBlock andFail:(RequestFail)failBlock{
    [[DRHttpTool shared] new_POST:DRURL(@"index.php/NewDietphan/getEnergy") andParams:@{@"token": [DRLoginDataModel getUserToken] ,@"type": @(type)} andSucc:^(id  _Nonnull data) {
        if (succBlock) {
            DREnergyTipModel *model = [DREnergyTipModel yy_modelWithJSON:data];
            succBlock(model);
        }
    } andFail:failBlock];
}

+(void)setEatFoodWithType:(NSInteger)type andMealType:(NSInteger)mealType andSuccess:(void (^)(NSString * ))succBlock andFail:(RequestFail)failBlock{
    NSDictionary *params = @{
                             @"token": [DRLoginDataModel getUserToken],
                             @"cycle": @(type),
                             @"ctcp" : [self getMealTypeWithIndex:mealType]
                             };
    [[DRHttpTool shared] new_POST:DRURL(@"index.php/NewDietphan/ajaxdietrecordsave") andParams:params andSucc:^(id  _Nonnull data) {
        if (succBlock) {
            succBlock(data[@"msg"]);
        }
        
    } andFail:failBlock];
}

+(void)delFoodWithFoodId:(NSString *)foodID andSuccess:(void (^)(void))succBlock andFail:(RequestFail)failBlock{
    [[DRHttpTool shared] new_POST:DRURL(@"index.php/NewDietphan/delFood") andParams:@{@"token": [DRLoginDataModel getUserToken] ,@"id": foodID} andSucc:^(id  _Nonnull data) {
        if (succBlock) {
            succBlock();
        }
    } andFail:failBlock];
}

+(void)setBlackListWithFoodId:(NSString *)foodID andDietId:(NSString *)dietId andSuccess:(void (^)(DRFoodMode *model))succBlock andFail:(RequestFail)failBlock{
    
    [[DRHttpTool shared] new_POST:DRURL(@"index.php/NewDietphan/setBlacklist")
                        andParams:@{
                                    @"id": foodID,
                                    @"type" : @"1",
                                    @"token": [DRLoginDataModel getUserToken],
                                    @"dietid" : dietId
                                    }
                        andSucc:^(id  _Nonnull data) {
                            if (succBlock) {
                                DRFoodMode *mode = [DRFoodMode yy_modelWithJSON:data];
                                succBlock(mode);
                            }
                        }
                        andFail:failBlock];
    
}

+(void)saveScheme:(NSInteger)type andName:(NSString *)name andSuccess:(void (^)(void))succBlock andFail:(RequestFail)failBlock{
    
    [[DRHttpTool shared] new_POST:DRURL(@"index.php/NewDietphan/saveScheme")
                        andParams:@{
                                    @"token":  [DRLoginDataModel getUserToken],
                                    @"type": @(type),
                                    @"name": name
                                    }
                          andSucc:^(id  _Nonnull data) {
                              if (succBlock) {
                                  succBlock();
                              }
                          }
                          andFail:failBlock];
}

+(void)getFriendListSuccess:(void (^)(NSArray<DRFriendModel *> * _Nonnull))succBlock andFail:(RequestFail)failBlock{
    [[DRHttpTool shared].manager POST:DRURL(@"index.php/NewUcenter/getPantryFriendList") parameters:@{@"token":  [DRLoginDataModel getUserToken]} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DRResponseModel *data = [DRResponseModel yy_modelWithJSON:responseObject];
        if (data.code == 200) {
            if (succBlock) {
                NSArray *datas = [NSArray yy_modelArrayWithClass:[DRFriendModel class] json:responseObject[@"datas"]];
                succBlock(datas);
            }
        }else{
            NSString *domain = @"com.DR.ErrorDomain";
            NSString *desc ;
            if ([data.datas[@"msg"] isKindOfClass:[NSNull class]]) {
                desc = @"请求失败!";
            }else{
                desc = NSLocalizedString(data.datas[@"msg"] ,nil);
            }
            NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : desc };
            NSError *error = [NSError errorWithDomain:domain
                                                 code:data.code
                                             userInfo:userInfo];
            failBlock(error);
        }

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
 
}

+(void)delDietFirendWithId:(NSString *)userId andSuccess:(void (^)(void))succBlock andFail:(RequestFail)failBlock{
    [[DRHttpTool shared] new_POST:DRURL(@"index.php/NewUcenter/delDietFirend")
                        andParams:@{
                                    @"token":  [DRLoginDataModel getUserToken],
                                    @"id": userId
                                    }
                          andSucc:^(id  _Nonnull data) {
                              if (succBlock) {
                                  succBlock();
                              }
                          }
                          andFail:failBlock];
}

+(void)matchingFoodInDayWithType:(NSInteger)type andSuccess:(void (^)(void))succBlock andFail:(RequestFail)failBlock{
    [[DRHttpTool shared] new_POST:DRURL(@"index.php/NewDietphan/food_matching_day")
                        andParams:@{
                                    @"token":  [DRLoginDataModel getUserToken],
                                    @"cycle": @(type)
                                    }
                          andSucc:^(id  _Nonnull data) {
                              if (succBlock) {
                                  succBlock();
                              }
                          }
                          andFail:failBlock];
}

+(void)familyDietphan:(NSString *)ctcp andSuccess:(void (^)(DRFamilyFoodPairingModel * _Nonnull))succBlock andFail:(RequestFail)failBlock{
    
    [[DRHttpTool shared] new_POST:DRURL(@"index.php/NewDietphan/family_catering")
                        andParams:@{
                                    @"token":  [DRLoginDataModel getUserToken],
                                    @"ctcp": ctcp
                                    }
                        andSucc:^(id  _Nonnull data) {
                            if (succBlock) {
                                DRFamilyFoodPairingModel *model = [DRFamilyFoodPairingModel yy_modelWithJSON:data];
                                succBlock(model);
                            }
                        }
                        andFail:failBlock];
    
}

+(void)autoChangeFood:(NSString *)foodId andSuccess:(void (^)(DRFoodMode * _Nonnull))succBlock andFail:(RequestFail)failBlock{
    
    [[DRHttpTool shared] new_POST:DRURL(@"index.php/NewDietphan/saveFood")
                        andParams:@{
                                    @"id": foodId,
                                    @"type" : @"1",
                                    @"token": [DRLoginDataModel getUserToken]
                                    }
                          andSucc:^(id  _Nonnull data) {
                              if (succBlock) {
                                  DRFoodMode *mode = [DRFoodMode yy_modelWithJSON:data];
                                  succBlock(mode);
                              }
                          }
                          andFail:failBlock];
    
}

+(void)existsRecord:(NSString *)ctcp andSuccess:(void (^)(BOOL))succBlock andFail:(RequestFail)failBlock{
    [[DRHttpTool shared] new_POST:DRURL(@"index.php/NewDietphan/existsRecord")
                        andParams:@{
                                    @"ctcp": ctcp,
                                    @"token" : [DRLoginDataModel getUserToken]
                                    }
                          andSucc:^(id  _Nonnull data) {
                            if (succBlock) {
                                succBlock(YES);
                            }
                          }
                          andFail:^(NSError * _Nonnull error) {
                              if (error.code == 400) {
                                  if (succBlock) {
                                      succBlock(NO);
                                  }
                              }else{
                                  if (failBlock) {
                                      failBlock(error);
                                  }
                              }
                          }];
}

+(NSString *)getMealTypeWithIndex:(NSInteger)index{
    switch (index) {
        case 0:
            return @"breakfast";
            break;
        case 1:
            return @"lunch";
            break;
        case 2:
            return @"dinner";
            break;
        default:
            return @"jia";
            break;
    }
}


@end
